package com.jswlw.schedule;

import com.jswlw.entity.biz.ApiFail;
import com.jswlw.service.biz.ApiFailService;
import com.jswlw.service.helper.HttpHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author 吴维斌 E-mail:wuweibin0817@163.com
 * @version 创建时间：${date} ${time}
 */
@Component
public class ApiSchedule {
    @Autowired
    ApiFailService apiFailService;
    @Autowired
    HttpHelper httpHelper;

    @Scheduled(cron = "0 0/2 * * * ?")
    public void execute() {
        List<ApiFail> apiFailList = apiFailService.query4List(new ApiFail());
        apiFailList.forEach(apiFail -> {
            if (!httpHelper.post(apiFail)) {
                apiFail.setFailCount(apiFail.getFailCount() + 1);
                apiFailService.update(apiFail);
            } else {
                apiFailService.delete(apiFail.getId());
            }
        });
    }
}
